using System;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;

namespace Nate.GUI.Canvas
{
    using Properties;

    public partial class NavMesh : Base
    {
        const string NavMeshXmlFileName = "NavMesh.xml";

        public NavMesh()
        {
            _xml  = new Xml();
            _Data = _xml.ReadFromFile(Xml.Constants.XmlFile);
            Components.Add(new Component.VertexMove(this));
            Components.Add(new Component.EdgeSplit(this));
            Components.Add(new Component.PolyInsert(this));
        }

        #region CanvasControlAbstract Overrides
        
        protected override ToolStripItem[] GenerateToolStripItems()
        {
            List<ToolStripItem> items = new List<ToolStripItem>();
            // ...
            return items.ToArray();
        }

        protected override void HandlePaint(object sender, PaintEventArgs e)
        {
            Graphics g              = e.Graphics;
            g.FillRectangle(Brushes.Wheat, new Rectangle(new Point(0,0), Size));
            /*
            GraphicsPath path = new GraphicsPath();
            foreach (Poly p in _Data.Polys)
            {
                path.StartFigure();

                List<PointF> points = new List<PointF>();
                foreach (Edge.ID edge_id in p.Edges)
                {
                    Edge edge = _Data.Get(edge_id);
                    PointF p1 = _Data.Get(edge.A).Point;
                    PointF p2 = _Data.Get(edge.B).Point;

                    if (!points.Contains(p1))
                    {
                        points.Add(p1);
                    }
                    if (!points.Contains(p2))
                    {
                        points.Add(p2);
                    }
                }

                path.AddPolygon(points.ToArray());
                g.FillPath(Brushes.Aqua, path);
                path.CloseFigure();
            }

            path.Dispose();*/

            foreach (Poly p in _Data.Polys)
            {
                List<PointF> points = new List<PointF>();
                foreach (Edge.ID edge_id in p.Edges)
                {
                    Edge edge = _Data.Get(edge_id);
                    PointF p1 = _Data.Get(edge.A).Point;
                    PointF p2 = _Data.Get(edge.B).Point;

                    if (!points.Contains(p1))
                    {
                        points.Add(p1);
                    }
                    if (!points.Contains(p2))
                    {
                        points.Add(p2);
                    }
                }

                g.FillPolygon(Brushes.Aqua, points.ToArray());
            }

            foreach (Edge edge in _Data.Edges)
            {
                g.DrawLine(edge.Highlighted ? Pens.Red : Pens.Black, _Data.Get(edge.A).Point, _Data.Get(edge.B).Point);
            }

            foreach (Vertex vertex in _Data.Vertices)
            {
                g.FillRectangle(vertex.Highlighted || vertex.Bordered ? Brushes.Red : Brushes.Green, vertex.Rect);
            }
        }

        #endregion

        public NavMesh.Data Bucket
        {
            get
            {
                return _Data;
            }
        }
        
        public Xml      XML
        {
            get
            {
                return _xml;
            }
        }

        Data _Data;
        Xml  _xml;
    }
}